﻿namespace ElencySolutions.ImageMap.Properties
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web.UI.WebControls;
    using EPiServer;
    using EPiServer.Core;
    using EPiServer.SpecializedProperties;
    using EPiServer.Web.PropertyControls;

    public class ImageMapEditor : SimplePage
    {
        private PropertyDataCollection _propertyDataCollection = new PropertyDataCollection();
        protected PlaceHolder InvalidDataPlaceHolder;
        protected PlaceHolder SilverlightPlaceHolder;
        protected PlaceHolder UrlPickerPlaceHolder;
        private string _width = string.Empty;
        private string _height = string.Empty;

        protected string PickerId
        {
            get 
            {
                if (UrlPickerPlaceHolder.Controls.Count > 0)
                    return UrlPickerPlaceHolder.Controls[0].ClientID;

                return string.Empty;
            }
        }

        protected string ImageUrl
        {
            get
            {
                string imageUrl = GetQueryStringValue("ImageUrl");

                if (!string.IsNullOrEmpty(imageUrl))
                {
                    if (!imageUrl.StartsWith("http://"))
                    {
                        string url = "http://" + Request.Url.Host;

                        if (Request.Url.Port != 80 && Request.Url.Port != 443)
                            url += ":" + Request.Url.Port;

                        if (!imageUrl.StartsWith("/"))
                            url += "/";

                        imageUrl = url + imageUrl;
                    }
                }

                return imageUrl;
            }
        }

        protected string Width
        {
            get
            {
                return !string.IsNullOrEmpty(_width) ? _width : GetQueryStringValue("Width");
            }
            set { _width = value; }
        }

        protected string Height
        {
            get
            {
                return !string.IsNullOrEmpty(_height) ? _height : GetQueryStringValue("Height");
            }
            set { _height = value; }
        }

        protected string AltText
        {
            get { return GetQueryStringValue("alt"); }
        }

        protected string XmlValueId
        {
            get { return GetQueryStringValue("XmlValueId"); }
        }

        protected string InfoBoxId
        {
            get { return GetQueryStringValue("infoBoxId"); }
        }

        protected string Mode
        {
            get
            {
                return GetQueryStringValue("mode");
            }
        }

        protected string DefaultLinkUrl
        {
            get
            {
                return GetQueryStringValue("DefaultLinkUrl");
            }
        }

        private string GetQueryStringValue(string keyName)
        {
            if (string.IsNullOrEmpty(Request.QueryString[keyName]))
                return string.Empty;

            return Request.QueryString[keyName].Trim();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Width.Length == 0 || Width == "0" || Height.Length == 0 || Height == "0")
            {
                try
                {
                    using (WebClient webClient = new WebClient())
                    {
                        using (Stream stream = webClient.OpenRead(ImageUrl))
                        {
                            System.Drawing.Image image = System.Drawing.Image.FromStream(stream);

                            if (Width.Length == 0 || Width == "0")
                                Width = image.Width.ToString();

                            if (Height.Length == 0 || Height == "0")
                                Height = image.Height.ToString();
                        }
                    }
                }
                catch
                {
                }
            }

            InvalidDataPlaceHolder.Visible = ImageUrl.Length == 0 || Width.Length == 0 || Height.Length == 0 || XmlValueId.Length == 0;
            SilverlightPlaceHolder.Visible = !InvalidDataPlaceHolder.Visible;

            if (SilverlightPlaceHolder.Visible)
            {
                try
                {
                    using (WebClient webClient = new WebClient())
                    {
                        webClient.DownloadData(ImageUrl);
                    }

                    _propertyDataCollection.Add("LinkUrl", new PropertyUrl());
                    PropertyData propertyData = _propertyDataCollection["LinkUrl"];
                    propertyData.Value = string.Empty;

                    IPropertyControl propertyControl = PropertyControlClassFactory.Instance.CreatePropertyControl(_propertyDataCollection["LinkUrl"]);
                    PropertyUrlControl urlPicker = propertyControl as PropertyUrlControl;
                    urlPicker.ID = "UrlPicker";
                    urlPicker.PropertyData = propertyData;
                    urlPicker.Properties = _propertyDataCollection;
                    urlPicker.RenderType = RenderType.Edit;
                    UrlPickerPlaceHolder.Controls.Add(urlPicker);
                    urlPicker.SetupControl();
                }
                catch (Exception)
                {
                    SilverlightPlaceHolder.Visible = false;
                    InvalidDataPlaceHolder.Visible = true;
                }
            }
        }

    }
}
